local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "gmsvrhelper"
local configdao = require "configdao"
local msgproxy = require "msgproxy"
local base = require "base"
local GmsvrCMD = {}

function GmsvrCMD.process(session, source, event, ...)
	local f = GmsvrCMD[event] 
	if f == nil then
		filelog.sys_error("GmsvrCMD.process invalid event:"..event)
		return nil
	end
	f(...)
end

function GmsvrCMD.start(conf)
	local server = msghelper:get_server()
	server.conf = conf

	msgproxy.init(skynet.getenv("svr_id"))

	msghelper:init_agent_pool(conf)
	msghelper:open_http_server(conf)

	base.skynet_retpack(true)
end

function GmsvrCMD.reload( ... )
	base.skynet_retpack(1)
	filelog.sys_error("GmsvrCMD.reload start")

	configdao.reload()
	skynet.sleep(200)
	local server = msghelper:get_server()
	local svrs = configdao.get_svrs("httpsvrs")
	local conf = svrs[skynet.getenv("svr_id")]
	if conf.gmhttpsvr_ip == server.conf.gmhttpsvr_ip 
		and conf.gmhttpsvr_port == server.conf.gmhttpsvr_port then
		return
	end
	msghelper:open_http_server(conf)

	server.conf = conf

	msgproxy.reload()
	
	filelog.sys_error("GmsvrCMD.reload end")
end

function GmsvrCMD.agentexit(id)
	msghelper:close_http_session(id)
end

return GmsvrCMD